<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Pricingtimeoptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('pricingtimeoption');
	}
	public function ajax_pricingtimeoption_get(){
		$pricingtimeoptionEntity =  new CI_Pricing_time_option_entity();
		$pricingtimeoption = $pricingtimeoptionEntity->getPricingTimeOptionByCode($_REQUEST['code']);
		echo json_encode($pricingtimeoption);
	}
	public function ajax_pricingtimeoption_list(){
		$pricingtimeoptionEntity =  new CI_Pricing_time_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$pricingtimeoptionList = $pricingtimeoptionEntity->getPricingTimeOptionListByLanguage($defaultLanguageId);
		$data['pricingtimeoptionList'] = $pricingtimeoptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/pricingtimeoptions/index', $data);
	}
	public function ajax_pricingtimeoption_save(){
		$pricingtimeoptionNameList = $_REQUEST['pricingtimeoption'];
		$pricingtimeoptionMeasureList = $_REQUEST['pricingtimeoptionmeasure'];
		$pricingtimeoptionOrderList = $_REQUEST['pricingtimeoptionorder'];
		$pricingtimeoptionNameDefault = '';
		foreach ($pricingtimeoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$pricingtimeoptionNameDefault = $value;
				break;
			}
		}
		$pricingtimeoptionMeasureDefault = '';
		foreach ($pricingtimeoptionMeasureList as $key => $value) {
			if($value != null && $value != ""){
				$pricingtimeoptionMeasureDefault = $value;
				break;
			}
		}
		$pricingtimeoptionOrderDefault = 0;
		foreach ($pricingtimeoptionOrderList as $key => $value) {
			if($value != null && $value != ""){
				$pricingtimeoptionOrderDefault = $value;
				break;
			}
		}
		
		$result = array();
		if($pricingtimeoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_pricingtimeoption_require');
		}else{
			$code = 0;
			foreach ($pricingtimeoptionNameList as $key => $value) {
				$pricingtimeoptionEntity =  new CI_Pricing_time_option_entity();
				if($value != null && $value != ''){
					$pricingtimeoptionEntity->setTime($value);
				}else{
					$pricingtimeoptionEntity->setTime($pricingtimeoptionNameDefault);
				}
				
				if($pricingtimeoptionMeasureList[$key] != null){
					$pricingtimeoptionEntity->setMeasure($pricingtimeoptionMeasureList[$key]);
				}else{
					$pricingtimeoptionEntity->setMeasure($pricingtimeoptionMeasureDefault);
				}
				
				if($pricingtimeoptionOrderList[$key] != null){
					$pricingtimeoptionEntity->setOrder($pricingtimeoptionOrderList[$key]);
				}else{
					$pricingtimeoptionEntity->setOrder($pricingtimeoptionOrderDefault);
				}
								
				$pricingtimeoptionEntity->setLanguageId($key);
				$pricingtimeoptionEntity->setCode($code);
				$id = $pricingtimeoptionEntity->save();
				if($code == 0){
					$code = $id;
					$pricingtimeoptionEntity->setId($id);
					$pricingtimeoptionEntity->setCode($code);
					$pricingtimeoptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_pricingtimeoption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_pricingtimeoption_update(){
		$pricingtimeoptionNameList = $_REQUEST['pricingtimeoption'];
		$pricingtimeoptionMeasureList = $_REQUEST['pricingtimeoptionmeasure'];
		$pricingtimeoptionOrderList = $_REQUEST['pricingtimeoptionorder'];
		$pricingtimeoptionIdList = $_REQUEST['pricingtimeoption_id'];
		$pricingtimeoptionCode = $_REQUEST['pricingtimeoption_code'];
		$pricingtimeoptionNameDefault = '';
		foreach ($pricingtimeoptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$pricingtimeoptionNameDefault = $value;
				break;
			}
		}
		$pricingtimeoptionMeasureDefault = '';
		foreach ($pricingtimeoptionMeasureList as $key => $value) {
			if($value != null && $value != ""){
				$pricingtimeoptionMeasureDefault = $value;
				break;
			}
		}
		$pricingtimeoptionOrderDefault = 0;
		foreach ($pricingtimeoptionOrderList as $key => $value) {
			if($value != null && $value != ""){
				$pricingtimeoptionOrderDefault = $value;
				break;
			}
		}
		$result = array();
		if($pricingtimeoptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_pricingtimeoption_require');
		}else{
			foreach ($pricingtimeoptionNameList as $key => $value) {
				$pricingtimeoptionEntity =  new CI_Pricing_time_option_entity();
				if($value != null && $value != ''){
					$pricingtimeoptionEntity->setTime($value);
				}else{
					$pricingtimeoptionEntity->setTime($pricingtimeoptionNameDefault);
				}
				if($pricingtimeoptionMeasureList[$key] != null){
					$pricingtimeoptionEntity->setMeasure($pricingtimeoptionMeasureList[$key]);
				}else{
					$pricingtimeoptionEntity->setMeasure($pricingtimeoptionMeasureDefault);
				}
				if($pricingtimeoptionOrderList[$key] != null){
					$pricingtimeoptionEntity->setOrder($pricingtimeoptionOrderList[$key]);
				}else{
					$pricingtimeoptionEntity->setOrder($pricingtimeoptionOrderDefault);
				}
				$pricingtimeoptionEntity->setLanguageId($key);
				$pricingtimeoptionEntity->setCode($pricingtimeoptionCode);
				$pricingtimeoptionEntity->setId($pricingtimeoptionIdList[$key]);
				$pricingtimeoptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_pricingtimeoption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_pricingtimeoption_delete(){
		$result = array();
		$pricingtimeoptionEntity =  new CI_Pricing_time_option_entity();
		$result['result'] = $pricingtimeoptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_pricingtimeoption_delete_success');
		}else{
			$result['message'] = lang('escort_pricingtimeoption_delete_error');
		}
		echo json_encode($result);
	}
}
?>